package com.example.zk.demo.config;

import com.example.zk.demo.utils.ZkClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.zookeeper.ZookeeperProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZkConfiguration {

    @Value("${spring.cloud.zookeeper.connect-string}")
    private String zookeeperServer;
    @Value(("${spring.cloud.zookeeper.session-timeout-ms}"))
    private int sessionTimeoutMs;
    @Value("${spring.cloud.zookeeper.connection-timeout-ms}")
    private int connectionTimeoutMs;
    @Value("${spring.cloud.zookeeper.max-retries}")
    private int maxRetries;
    @Value("${spring.cloud.zookeeper.base-sleep-time-ms}")
    private int baseSleepTimeMs;

    @Bean(initMethod = "init", destroyMethod = "stop")
    public ZkClient zkClient(ZookeeperProperties zookeeperProperties) {
        ZkClient zkClient = new ZkClient(zookeeperProperties);
        zkClient.setZookeeperServer(zookeeperServer);
        zkClient.setSessionTimeoutMs(sessionTimeoutMs);
        zkClient.setConnectionTimeoutMs(connectionTimeoutMs);
        zkClient.setMaxRetries(maxRetries);
        zkClient.setBaseSleepTimeMs(baseSleepTimeMs);
        return zkClient;
    }


}
